package com.solitaire.and;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.applovin.sdk.AppLovinMediationProvider;
import com.applovin.sdk.AppLovinPrivacySettings;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkInitializationConfiguration;

public class SoliSDKManager implements Application.ActivityLifecycleCallbacks {

    public static SoliSDKManager getInstance() {
        return InstanceHolder.GAME_APPLICATION;
    }

    private static class InstanceHolder {
        private static final SoliSDKManager GAME_APPLICATION = new SoliSDKManager();
    }

    public void init(Activity activity) {
        Context mApplication = activity.getApplicationContext();
        ((Application) mApplication).registerActivityLifecycleCallbacks(this);
        SoliCocosBridge.getInstance().init(mApplication);

        setPrivacySettings(mApplication);
        init(mApplication);

    }

    private void init(Context mApplication) {
        AppLovinSdk.getInstance(mApplication).initialize(getInitConfig(mApplication), config -> {
//                AppLovinSdk.getInstance(context).showMediationDebugger();
            SoliMaxReward.getInstance().loadRewardAd(mApplication);
        });
    }

    private AppLovinSdkInitializationConfiguration getInitConfig(Context mApplication) {
        return AppLovinSdkInitializationConfiguration.builder(
                        StringEncryptor.decrypt(SoliConstant.MAX_KEY), mApplication)
                .setMediationProvider(AppLovinMediationProvider.MAX)
                .build();
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
        try {
            onActivityCreated(activity);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void onActivityCreated(Activity activity) {
        if (activity.getClass().getName().equals(SoliConstant.GAME_MAIN_CLS)) {
            SoliCocosBridge.getInstance().setMainGameActivity(activity);
        }
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {

    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {

    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {

    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {

    }

    private void setPrivacySettings(Context mApplication) {
        AppLovinPrivacySettings.setHasUserConsent(true, mApplication);
        AppLovinPrivacySettings.setDoNotSell(false, mApplication);
    }

}
